let express = require('express');
let router = express.Router();

const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

// 实体类
const Response = require('../entity/http/Response');
const PostImg = require('../entity/circlePost/PostImg');
const CirclePost = require('../entity/circlePost/CirclePost');
const PostComment = require('../entity/circlePost/PostComment');
const PostCommentRequest = require('../entity/circlePost/PostCommentRequest');
const ObjId = require('../entity/common/ObjId');

// 业务层
const circlePostService = require('../service/circlePostService');

/*
 * 上传动态图片接口
 */
router.post('/uploadCirclePostImg', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let postImg = new PostImg(req);
    // 判断参数是否合法
    let flag = postImg.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.uploadCirclePostImg(uid, postImg).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("上传动态图片失败"));
    });
});

/*
 * 预发布动态接口
 */
router.post('/preAddCirclePost', async function (req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let circlePost = new CirclePost(req);
    // 判断参数是否合法
    let flag = circlePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.preAddCirclePost(uid, circlePost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("上传动态图片失败"));
    });
});

/*
 * 发布动态
 * 发布的用户、标题、文字内容、图片列表、标签、发布时间
 */
router.post('/addCirclePost', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let circlePost = new ObjId(req.body, 'circlePostId');
    // 判断参数是否合法
    let flag = circlePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.addCirclePost(uid, circlePost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("发布动态失败"));
    });
});

/*
 * 查看动态详情
 * 获取动态的内容的同时，还要获取点赞数、评论数、以及当前用户是否点赞了该动态
 */
router.get('/getPostInfo', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let circlePost = new ObjId(req.query, 'id');
    // 判断参数是否合法
    let flag = circlePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.getPostInfo(uid, circlePost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取动态详情失败"));
    });
});

/*
 * 删除动态
 */
router.post('/delPost', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let circlePost = new ObjId(req.body, 'postId');
    // 判断参数是否合法
    let flag = circlePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.delPost(uid, circlePost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("删除动态失败"));
    });
});

/*
 * 点赞动态
 */
router.post('/likePost', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let circlePost = new ObjId(req.body, 'postId');
    // 判断参数是否合法
    let flag = circlePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.likePost(uid, circlePost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("点赞动态失败"));
    });
});

/*
 * 取消点赞动态
 */
router.post('/nolikePost', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let circlePost = new ObjId(req.body, 'postId');
    // 判断参数是否合法
    let flag = circlePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.nolikePost(uid, circlePost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("取消点赞动态失败"));
    });
});

/*
 * 评论动态
 */
router.post('/addCommentPost', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let postComment = new PostComment(req);
    let flag = postComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.addCommentPost(uid, postComment).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("评论动态失败"));
    });
});

/*
 * 删除评论
 */
router.post('/delCommentPost', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let postComment = new ObjId(req.body, 'postCommentId');
    // 判断参数是否合法
    let flag = postComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.delCommentPost(uid, postComment).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("删除评论失败"));
    });
});

/*
 * 举报动态
 */
router.post('reportPost', async function(req, res, next) {});

/*
 * 推荐动态列表
 */
router.post('/getRecommendPostList', async function(req, res, next) {
    // 读取请求参数
    let { uid } = req.jwtResult.data;
    let lastPost = new ObjId(req.body, 'lastPostId', true);
    // 判断参数是否合法
    let flag = lastPost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.getRecommendPostList(uid, lastPost).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取推荐动态列表失败"));
    });
});

/*
 * 懒加载获取动态评论
 */
router.post('/getPostCommentList', async function(req, res, next) {
    // 读取请求参数
    let postCommentRequest = new PostCommentRequest(req);
    // 判断参数是否合法
    let flag = postCommentRequest.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.getPostCommentList(postCommentRequest).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取动态评论失败"));
    });
});

// 获取动态图片预签名URL接口
router.get('/getCirclePostImgUrl', async function(req, res, next) {
    // 读取请求参数
    let img = new ObjId(req.query, 'imgId');
    // 判断参数是否合法
    let flag = img.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    circlePostService.getCirclePostImgUrl(img.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取动态图片预签名URL失败"));
    });
});

// 获取所有动态类型接口
router.get('/getAllCirclePostType', async function(req, res, next) {
    circlePostService.getAllCirclePostType().then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取所有动态类型失败"));
    });
});

module.exports = router;
